# this script creates a network graph from ownership links
# previously extracted from JSCReg

# load the list of ownership links from JSCReg as of specific date
all_entities <- read_dta(paste0("Ownership Data/all_entities_fin_", 
                                  search_date, 
                                  "_full_sample_JSCReg_only.dta"), 
                           encoding = "UTF-8")

# recode some variables
all_entities <- all_entities %>%
  rename(entity_name = name) %>%
  # oligarch dummy not needed in this analysis (all are missing for JSCReg)
  select(-c(oligarch)) %>%
  mutate(entity_name_latin = stri_trans_general(entity_name, "Ukrainian-Latin/BGN"),
         country = as.integer(country),
         # create a unified id based on okpo and country (Ukrainian/foreign)
         # by adding a two-letter prefix to each numeric id
         foreign_id = ifelse(entity == 1 & country != 804, paste0("FC", id), NA),
         id = ifelse(country == 804, 
                     paste0("UC", id),
                     paste0("FC", country)),
         prev_id = ifelse(!is.na(prev_id),
                          paste0("UC", prev_id),
                          NA),
         # assign status
         entity = ifelse(entity == 1, "corporate", "individual"),
         owner = ifelse(is.na(owner), 0, owner)) 

# extract all vertices (firms or individuals)
all.ids <- all_entities %>% 
  arrange(id, entity, country, step) %>%
  distinct(id, entity, country, .keep_all = T)

# create a list of edges from ownership links
# each edge is owner id + subsidiary id
all.edges <- all_entities %>% 
  mutate(owner = id, sub = prev_id) %>%
  dplyr::select(owner, sub) %>%
  arrange(owner, sub) %>%
  drop_na(sub) %>%
  distinct(owner, sub, .keep_all = T)

# create directed network from edges
o.network.D <- graph.data.frame(all.edges, directed = T, vertices = all.ids)

# calculate some basic stats for the nodes
V(o.network.D)$degree <- igraph::degree(o.network.D)
V(o.network.D)$degree.out <- igraph::degree(o.network.D, mode="out")
V(o.network.D)$betweenness <- estimate_betweenness(o.network.D, directed = F, cutoff=1000000)

